package com.example.androidrssreader;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	public final static String PACKAGE_NAME = "com.example.androidrssreader";

	Button btnReadRss;
	EditText edtRssUrl;
	ListView listRssItems;
	ArrayList<RssItem> rssItems;
	ProgressBar progressBar;
	RssListAdapter listAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		this.progressBar = (ProgressBar) this.findViewById(R.id.progressBar);
		progressBar.setVisibility(View.INVISIBLE);
		
		this.edtRssUrl = (EditText) this.findViewById(R.id.edtRssUrl);
		
		this.listRssItems = (ListView) this.findViewById(R.id.listRssItems);
		this.listRssItems.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapter, View view, int position, long arg3) {
				Toast.makeText(MainActivity.this, position, Toast.LENGTH_LONG).show();
			}
			
			
		} );
		
		this.btnReadRss = (Button) this.findViewById(R.id.btnReadRss);
		btnReadRss.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// Clearing all ListView data
				rssItems = new ArrayList<RssItem>();
				listAdapter = new RssListAdapter(MainActivity.this, R.layout.rss_list_item, rssItems);
				listRssItems.setAdapter(listAdapter);

				// Creating AsyncTask RSS reader which will read RSS items and update RssListAdapter
				RssAsyncReader rssReader = new RssAsyncReader(MainActivity.this, listAdapter, rssItems);
				rssReader.execute(edtRssUrl.getText().toString());
			}
			
		});
		btnReadRss.setFocusableInTouchMode(true);
		btnReadRss.requestFocus();
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
